package com.shop2cn.iapi.sqrefund.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.resp.Order;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqrefund.parameter.AuditRefundReq;
import com.shop2cn.iapi.sqrefund.parameter.bean.RefundAddressVo;
import com.shop2cn.iapi.sqrefund.parameter.enums.CurrentMchRefundStatusEnum;
import com.shop2cn.iapi.sqrefund.service.AuditRefundCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.RefundService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqrefund.RefundBill;
import com.ymttest.database.model.sqrefund.RefundBillIndex;
import com.ymttest.database.sqlwapper.sqrefund.RefundBillIndexWrapper;
import com.ymttest.database.sqlwapper.sqrefund.RefundBillWrapper;

/**
 * 发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AuditRefund {

	private static AuditRefundReq auditRefundReq;
	private static AuditRefundCall auditRefundCall;
	
	private static long autotest_goodsOwnerId = ShenqiUserSet.autotest_goodsOwnerId;
	
	private static RefundBillWrapper refundBillWrapper = new RefundBillWrapper();
	private static RefundBillIndexWrapper refundBillIndexWrapper = new RefundBillIndexWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退款审核");
	}

	@Before
	public void caseUp() {

		auditRefundReq = new AuditRefundReq();
		auditRefundCall = new AuditRefundCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(auditRefundCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify(Long orderId) {
		
		RefundBill actualRefundBill = refundBillWrapper.selectByRefundNoAndOrderId(orderId, auditRefundReq.getRefundNo());
		
		if (null != auditRefundReq.getRefundAddress()) {
			
			Logger.verifyEquals(auditRefundReq.getPass() ? 21 : 23,
					actualRefundBill.getRefund_status(), "验证Refund_status");
			Logger.verifyNotNull(actualRefundBill.getRefund_address(), "验证Refund_address");
		} else {
			
			Logger.verifyEquals(auditRefundReq.getPass() ? 2 : 1,
					actualRefundBill.getRefund_status(), "验证Refund_status");
		}
		
		
		RefundBillIndex actualRefundBillIndex = refundBillIndexWrapper
				.selectByRefundNoAndMerchantId(actualRefundBill.getRefund_no(),
						actualRefundBill.getSeller_id());

		Logger.verifyEquals(actualRefundBill.getRefund_status(),
				actualRefundBillIndex.getRefund_status(), "校验Refund_status");
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AuditRefund_001() {
		Logger.start(false, "买家申请退款，货头审核成功");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);
			
			String refundNo = RefundService.createFullRefundByBuyerId(orderId, userId);
			
			auditRefundReq.setMerchantId(autotest_goodsOwnerId);
			auditRefundReq.setPass(true);
			auditRefundReq.setRefundNo(refundNo);
//			auditRefundReq.setRefundNo("TH210702000161097");
			auditRefundReq.setReviewerId(autotest_goodsOwnerId);
			auditRefundReq.setCurrentMchRefundStatus(CurrentMchRefundStatusEnum.LEVEL_TWO.getCode());

			auditRefundCall.setData(auditRefundReq);
			auditRefundCall.callService();
			
//			this.verify(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AuditRefund_002() {
		Logger.start(false, "买家申请退款，货头审核拒绝");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId();

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);
			
			String refundNo = RefundService.createFullRefundByBuyerId(orderId, userId);
			
			auditRefundReq.setMerchantId(500002490L);
			auditRefundReq.setPass(false);
			auditRefundReq.setRefundNo(refundNo);
			auditRefundReq.setReviewerId(500002490L);
			auditRefundReq.setCurrentMchRefundStatus(CurrentMchRefundStatusEnum.LEVEL_ONE.getCode());

			auditRefundCall.setData(auditRefundReq);
			auditRefundCall.callService();
			
			this.verify(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AuditRefund_003() {
		Logger.start(false, "买家申请退货退款，货头审核地址成功");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false,
							false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId);

			Order order = placeOrderResp.getOrderList().get(0);

			Long userId = placeOrderResp.getUserId(); 

			long orderId = order.getOrderId();

			BigDecimal amount = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					amount.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);
			
			// 卖家接单
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId,
					orderId);

			// 发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					null, null);
			
			String refundNo = RefundService.createFullRefundGoods(orderId, userId);
			
			auditRefundReq.setMerchantId(500002490L);
			auditRefundReq.setPass(true);
			auditRefundReq.setRefundNo(refundNo);
			auditRefundReq.setReviewerId(500002490L);
			auditRefundReq.setCurrentMchRefundStatus(CurrentMchRefundStatusEnum.LEVEL_ONE.getCode());
			
			RefundAddressVo refundAddressVo = RefundAddressVo.buildStandardRefundAddress();
						
			auditRefundReq.setRefundAddress(refundAddressVo);

			auditRefundCall.setData(auditRefundReq);
			auditRefundCall.callService();
			
			this.verify(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);  
		}
	}
}
